#ifndef JSONRPCHANDLERIMPL_H
#define JSONRPCHANDLERIMPL_H

#include "IProtocolHandler.h"
#include "JsonRpcMessageParser.h"
#include "JsonRpcMessageSerializer.h"
#include <QScopedPointer>

/**
 * @brief JSON-RPC handler implementation
 * @details Main protocol handler that coordinates parsing and serialization for JSON-RPC protocol
 */
class JsonRpcHandlerImpl : public IProtocolHandler
{
public:
    explicit JsonRpcHandlerImpl();
    ~JsonRpcHandlerImpl() override;

public:
    void setMessageParser(std::unique_ptr<IMessageParser> parser);
    void setMessageSerializer(std::unique_ptr<IMessageSerializer> serializer);

public:// IProtocolHandler
    QJsonObject parseRequest(const QByteArray& data) override;
    QByteArray serializeResponse(const QJsonObject& response) override;
    QJsonObject createErrorResponse(int code, const QJsonValue& message = QJsonValue()) override;

private:
    /**
     * @brief Process a single JSON-RPC request.
     * Handles individual request processing including method dispatch,
     * parameter validation, and result generation.
     * @param request - JSON object containing the request data
     * @return QJsonObject - Response object containing result or error
     */
    QJsonObject processSingleRequest(const QJsonObject& request);

private:
    std::unique_ptr<IMessageParser> mMessageParser;
    std::unique_ptr<IMessageSerializer> mMessageSerializer;
};

#endif // JSONRPCHANDLERIMPL_H
